var vo={};
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listDevice").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight);
	$("#isflag").val("");
	$("#all").addClass("a_hover");
	var fcol=opt.split('</a>');
	var contrlColumn=0;
	if(fcol.length>0){
		contrlColumn=(fcol.length-1)*window.fcolMinWidth;
	}
	$("#listDevice").datagrid({
					url:'/device/getDevice',
					title:'设备列表',
					method:'get',
					pagination:true,
					pageList: window.PageSize,
					loadMsg:'加载中......',
					nowrap:true,
					view: detailview,
					rowStyler:function(index,row){   
				        if (row.isflag){   
				            return 'background-color:'+window.flagColor;   
				        }
				        if(row.status=='02'){
				        	return 'background-color:yellow';
				        }
				    },
					columns:[[
					         {field:'ck',checkbox:true},
							 {field:'rowid' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
								 var re=new RegExp("{param}","g");
								 var ref=opt.replace(re,row.deviceid);
								 return ref;
							 }},
							 {field:'deviceid',title:'设备编号', align:'left',width:100,sortable:true},
					         {field:'devicename',title:'设备名称', align:'left',width:100,sortable:true},
					         {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
					         {field:'shopname',title:'门店名称', align:'left',width:100,sortable:true},
					         {field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
					         {field:'dealername',title:'经销商名称', align:'left',width:100,sortable:true},
					         {field:'modelname',title:'设备型号', align:'left',width:100,sortable:true},
					         {field:'deviceprice',title:'设备价格',align:'left',width:100},
					         {field:'statusname',title:'健康状况',align:'left',width:100},
					         {field:'isflag',title:'状态',align:'left',width:80,formatter:function(value,row,index){
					        	if(value){
					        		return '已停用';
					        	}
					        	else{
					        		return '已启用';
					        	}
					         }},
					         {field:'buildtime',title:'安装时间',align:'left',width:100,formatter:function(value,row,index){
					        	 if(value==''){
					        		 return '';
					        	 }
					        	 var datetime=Common.formatterDate(value);
					        	 return datetime;
					         }},
					         {field:'remark',title:'备注',align:'left',width:120,formatter:function(value, row, index){
					        	 if(value==undefined){
						 				return '';
						 			}
						 			else if(value.length<13){
						 				return value;
						 			}else{
						 				return value.substr(0, 13) + '...';
						 			}
						      }}
					     ]],
					     detailFormatter:function(index,row){
					            return '<div style="padding:2px"><table id="deviceview' + index + '"></table></div>';
					     },
					     onExpandRow:function(index,row){
					    	 var id=row.deviceid;
					    	 $("#deviceview"+index).datagrid({
					    		 	url:'/device/getHRA',
					    		 	queryParams:{'deviceid':id},
									title:'体检列表',
									method:'get',
									pagination:true,
									pageList: window.subPageSize,
									loadMsg:'加载中......',
									rowStyler:function(index,row){   
								        if (row.isflag){   
								        	return 'background-color:'+window.flagColor;
								        }   
								    },
								    columns:[[
												 {field:'did' ,align:'center',title:'操作',width:contrlColumn,formatter: function(value, row, index){
													 var re='<a href="javascripts: void();" onclick="viewHRA(\''+value+'\')" class="a_control">详情</a>&nbsp;';
													 return re;
												 }},
												 {field:'username',title:'用户名称', align:'left',width:100},
												 {field:'huiyuanziliao_jianchariqi',title:'检查日期',width:80,align:'center',sortable:true},
												 {field:'devicename',title:'设备名称', align:'left',width:100,sortable:true},
										         {field:'shopname',title:'设备门店', align:'left',width:100,sortable:true},
										         {field:'modelname',title:'设备型号',align:'left',width:80},
										         {field:'rtc_shengao',title:'身高',align:'left',width:50},
										         {field:'rtc_tizhong',title:'体重',align:'left',width:50},
										         {field:'rtc_bmi',title:'bmi指数',align:'left',width:50},
										         {field:'rtc_gaoya',title:'高压',align:'left',width:50},
										         {field:'rtc_diya',title:'低压',align:'left',width:50},
										         {field:'rtc_xinlv',title:'心率',align:'left',width:50},
										         {field:'rtc_xueyang',title:'血氧',align:'left',width:50}
//										         {field:'isflag',title:'状态',align:'left',width:80,formatter:function(value,row,index){
//										        	if(value){
//										        		return '已停用';
//										        	}
//										        	else{
//										        		return '已启用';
//										        	}
//										         }}
										     ]],
								    onResize:function(){  
						                    $('#listDevice').datagrid('fixDetailRowHeight',index);  
						                },
					                onLoadSuccess:function(){  
					                    setTimeout(function(){  
					                        $('#listDevice').datagrid('fixDetailRowHeight',index);  
					                    },0);  
					                },
					    	 });
					    	 $('#listDevice').datagrid('fixDetailRowHeight',index);
					     }
				});
	$("#devicemodel").combobox({
		 url:'/model/getCombobox',
		 valueField:'id',
		 textField:'text',
	});
	$("#devicestatus").combobox({
		 url:'/device/getStatus',
		 valueField:'id',
		 textField:'text',
	});
	$("#serbutton").click(function(){
		vo.isflag=$("#isflag").val();
		vo.devicename=$("#devicename").val();
		vo.deviceid=$("#deviceid").val();
		vo.shopid=$("#shopid").val();
		vo.shopname=$("#shopname").val();
		vo.dealerid=$("#dealerid").val();
		vo.dealername=$("#dealername").val();
		vo.modelid=$('#devicemodel').combobox('getValue');
		vo.status=$('#devicestatus').combobox('getValue');
		$("#listDevice").datagrid('load',vo);
	});
	if(opt==''){
		$("#listDevice").datagrid('hideColumn','rowid');
	}
	$("#listDevice").datagrid({
		toolbar: '#tb'
	});
});
/**
 * 按状态查询
 * @param status
 */
function searchStatus(status){
	var sta='';
	if(status=="all"){
		$("#all").addClass("a_hover");
		removeClass($("#enable"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==false){
		sta=false;
		$("#enable").addClass("a_hover");
		removeClass($("#all"),"a_hover");
		removeClass($("#disable"),"a_hover");
	}
	if(status==true){
		sta=true;
		removeClass($("#all"),"a_hover");
		removeClass($("#enable"),"a_hover");
		$("#disable").addClass("a_hover");
	}
	$("#isflag").val(sta);
	vo.isflag=sta;
	$("#listDevice").datagrid('load',vo);
}
/**
 * 批量启用
 * @param idcard
 */
function betchEnable(){
	var rows=$("#listDevice").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('启用','您确定要启用选中的设备吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].deviceid+',';
			}
			$.ajax({
				type:'get',
				url: '/device/betchEnable',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","启用成功",'info',function(){
							$("#listDevice").datagrid('load',vo);
						});
						
					}
					else{
						$.messager.alert("失败","启用失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","启用失败：",'error');
				}
			});
		}
	})
}
/**
 * 批量停用
 * @param idcard
 */
function betchDisable(){
	var rows=$("#listDevice").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('停用','您确定要停用选中的设备吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].deviceid+',';
			}
			$.ajax({
				type:'get',
				url: '/device/betchDisable',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","停用成功",'info',function(){
							$("#listDevice").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","停用失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","停用失败：",'error');
				}
			});
		}
	})
}
/**
 * 批量删除
 * @param idcard
 */
function betchDel(){
	var rows=$("#listDevice").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.confirm('删除','您确定要删除选中的设备吗？',function(data){
		if(data){
			var ids='';
			for(var i=0;i<rows.length;i++){
				ids+=rows[i].deviceid+',';
			}
			$.ajax({
				type:'get',
				url: '/device/betchDel',
				data:{ids:ids},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listDevice").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 删除
 * @param idcard
 */
function delDevice(id){
	$.messager.confirm('删除','您确定要删除选中的设备吗？',function(data){
		if(data){
			$.ajax({
				type:'get',
				url: '/device/delDevice',
				data:{id:id},
				success:function(data){
					if(data.code==200){
						$.messager.alert("成功","删除成功",'info',function(){
							$("#listDevice").datagrid('load',vo);
						});
					}
					else{
						$.messager.alert("失败","删除失败："+data.msg,'error');
					}
				},
				error:function(data){
					$.messager.alert("失败","删除失败：",'error');
				}
			});
		}
	})
}
/**
 * 查看
 */
function viewDevice(id){
	window.parent.openDialog("设备详情",'/device/viewDevice','',400,580,'id='+id);
}
/**
 * 新增
 */
function addDevice(){
	window.parent.openDialog("新增设备",'/device/addDevice','',400,500,'');
}
/**
 * 复制
 */
function copyDevice(id){
	window.parent.openDialog("新增设备",'/device/copyDevice','',400,500,'id='+id);
}
/**
 * 编辑
 * @param id
 */
function edit(id){
	window.parent.openDialog("修改设备",'/device/editDevice','',400,500,'id='+id);
}
/**
 * 查看HRA数据
 * @param id
 */
function viewHRA(id){
	window.parent.opentab('01001','HRA数据详情','/hra/view','id='+id);
}
/**
 * 导出为excel
 */
function exportXls(){
	vo.shopid=getVal(vo.shopid);
	vo.shopname=getVal(vo.shopname);
	vo.dealerid=getVal(vo.dealerid);
	vo.dealername=getVal(vo.dealername);
	vo.deviceid=getVal(vo.deviceid);
	vo.devicename=getVal(vo.devicename);
	vo.modelid=getVal(vo.modelid);
	vo.status=getVal(vo.status);
	vo.isflag=getVal(vo.isflag);
	var url='/device/exportXls?shopid='+vo.shopid+'&shopname='+vo.shopname+'&dealerid='+
	vo.dealerid+'&dealername='+vo.dealername+'&deviceid='+vo.deviceid+
	'&devicename='+vo.devicename+'&modelid='+vo.modelid+'&status='+vo.status+'&isflag='+vo.isflag;
	window.open(url,'','',false);
}